#!/usr/bin/env node

var program = require('commander');
var fs = require('fs');

//定义参数,以及参数内容的描述
program
    .version('0.0.1')
    .usage('[options] [value ...]')
    .option('-n, --name <string>', 'no')
    .option('-a, --alias <string>', '未命名')
    .parse(process.argv)

let name = program.name
let alias = program.alias

let vuexPath = './src/vuex/modules/' + name + '.js'
let routesPath = './src/routes/modules/' + name + '.js'
let apiPath = './src/vuex/api/' + name + '.js'
let pagesPath = './src/components/backend/pages/' + name
let arr = ['/Add.vue', '/Update.vue', '/Index.vue']

fs.stat(pagesPath, function(err, stat){
  if(stat) {
    console.log('pages下的目录已存在');
    return 
  } else {
    fs.mkdirSync(pagesPath, 0755)
    arr.map(y => {
      fs.open(pagesPath + y, 'wx', (err, fd) => {
        if (err) {
          if (err.code === 'EEXIST') {
            console.error('文件已存在');
            return;
          }
      
          throw err;
        }
      
        fs.write(fd, '<template>\n\n</template>\n<script>\nexport default {\n\n}\n</script>\n<style>\n\n</style>', function(err, a, b) {
          console.log(err)
        })
        console.log('pages 已写入')
      });
    })
  }
})


fs.open(vuexPath, 'wx', (err, fd) => {
  if (err) {
    if (err.code === 'EEXIST') {
      console.error('vues下的文件已存在');
      return;
    }

    throw err;
  }

  fs.write(fd, `import api from '../api'\n\nconst state = {\n}\n\nconst  getters = {\n}\n\nconst actions = {\n  get${name}({}, param) {\n   return api.get(api.url('get${name}'), param)\n  },\n  find${name}({}, id) {\n   return api.get(api.url('find${name}', id))\n  },\n  update${name}({}, param) {\n    return api.put(api.url('update${name}', param.id), param)\n },\n  delete${name}({}, id) {\n    return api.delete(api.url('delete${name}', id))\n  },\n  create${name}({}, param) {\n    return api.post(api.url('create${name}'), param)\n  }\n}\nconst mutations = {\n}\n\nexport default {\nstate,\ngetters,\nactions,\nmutations\n}`, function(err, a, b) {
    console.log(err)
  })
  console.log('vuex 已写入')
});

fs.open(routesPath , 'wx', (err, fd) => {
  if (err) {
    if (err.code === 'EEXIST') {
      console.error('routes下的文件已存在');
      return;
    }

    throw err;
  }

  fs.write(fd, `const Index = () =>import('~/components/backend/pages/${name}/Index.vue')\nconst Add = () =>import('~/components/backend/pages/${name}/Add.vue')\nconst Update = () =>import('~/components/backend/pages/${name}/Update.vue')\n\nexport default [\n  {path: '/${name}', name: '${name}', component: Index, meta: {title: '${alias}管理'}},\n  {path: '/${name}/add', name: '${name}.add', component: Add, meta: {title: '新增${alias}'}},\n  {path: '/${name}/update/:id', name: '${name}.update', component: Update, meta: {title: '${alias}修改'}}\n]`, function(err, a, b) {
    console.log(err)
  })
  console.log('routes 已写入')
});

fs.open(routesPath , 'wx', (err, fd) => {
  if (err) {
    if (err.code === 'EEXIST') {
      console.error('routes下的文件已存在');
      return;
    }

    throw err;
  }

  fs.write(fd, `const Index = () =>import('~/components/backend/pages/${name}/Index.vue')\nconst Add = () =>import('~/components/backend/pages/${name}/Add.vue')\nconst Update = () =>import('~/components/backend/pages/${name}/Update.vue')\n\nexport default [\n  {path: '/${name}', name: '${name}', component: Index, meta: {title: '${alias}管理'}},\n  {path: '/${name}/add', name: '${name}.add', component: Add, meta: {title: '新增${alias}'}},\n  {path: '/${name}/update/:id', name: '${name}.update', component: Update, meta: {title: '${alias}修改'}}\n]`, function(err, a, b) {
    console.log(err)
  })
  console.log('routes 已写入')
});

fs.open(apiPath , 'wx', (err, fd) => {
  if (err) {
    if (err.code === 'EEXIST') {
      console.error('api下的文件已存在');
      return;
    }

    throw err;
  }

  fs.write(fd, `export default {\n  'get${name}': '/${name}s',\n  'find${name}': '/${name}s/{1}',\n  'update${name}': '/${name}s/{1}',\n  'delete${name}': '/${name}s/{1}',\n  'create${name}': '/${name}s',\n}`, function(err, a, b) {
    console.log(err)
  })
  console.log('api 已写入')
});
